﻿using MDK.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform.UserControl.Config
{
    public partial class CameraSet : BaseForm
    {
        private string _MonikerString;
        private string _DeviceName;
        public CameraSet()
        {
            InitializeComponent();
        }
        public CameraSet(string MonikerString,string DeviceName) :this()
        {
            _MonikerString = MonikerString;
            _DeviceName = DeviceName;
        }
        private void CameraSet_Load(object sender, EventArgs e)
        {
            InitTrackBar();
            GetCameraSet();
        }

        private void InitTrackBar()
        {
            track1.SetTrackValue(_MonikerString, VideoProcAmpProperty.Brightness, -64, 64);
            track2.SetTrackValue(_MonikerString, VideoProcAmpProperty.Contrast, 0, 95);
            track3.SetTrackValue(_MonikerString, VideoProcAmpProperty.Hue, -2000, 2000);
            track4.SetTrackValue(_MonikerString, VideoProcAmpProperty.Saturation, 0, 100);
            track5.SetTrackValue(_MonikerString, VideoProcAmpProperty.Sharpness, 0, 7);
            track6.SetTrackValue(_MonikerString, VideoProcAmpProperty.Gamma, 100, 300);
            track7.SetTrackValue(_MonikerString, VideoProcAmpProperty.WhiteBalance, 2800, 6500);
            track8.SetTrackValue(_MonikerString, VideoProcAmpProperty.BacklightCompensation, 0, 1);
            track9.SetTrackValue(_MonikerString, VideoProcAmpProperty.Gain, 0, 100);
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            CameraParameter parameter = new Config.CameraParameter()
            {
                MonikerString = _MonikerString,
                CameraName = _DeviceName,
                Brightness = decimal.ToInt32(track1.EditValue),
                Contrast = decimal.ToInt32(track2.EditValue),
                Hue = decimal.ToInt32(track3.EditValue),
                Saturation = decimal.ToInt32(track4.EditValue),
                Sharpness = decimal.ToInt32(track5.EditValue),
                Gamma = decimal.ToInt32(track6.EditValue),
                WhiteBalance = decimal.ToInt32(track7.EditValue),
                BacklightCompensation = decimal.ToInt32(track8.EditValue),
                Gain = decimal.ToInt32(track9.EditValue),
            };
            SaveCameraSet(parameter);
            this.DialogResult = DialogResult.OK;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void GetCameraSet()
        {
            string fp = Application.StartupPath + "\\CameraSet.json";
            if (File.Exists(fp))
            {
                var parameter = JsonConvert.DeserializeObject<List<CameraParameter>>(File.ReadAllText(fp)).FirstOrDefault(t => t.CameraName == _DeviceName);
                if (parameter == null) return;
                track1.EditValue = parameter.Brightness;
                track2.EditValue = parameter.Contrast;
                track3.EditValue = parameter.Hue;
                track4.EditValue = parameter.Saturation;
                track5.EditValue = parameter.Sharpness;
                track6.EditValue = parameter.Gamma;
                track7.EditValue = parameter.WhiteBalance;
                track8.EditValue = parameter.BacklightCompensation;
                track9.EditValue = parameter.Gain;
            }
        }

        private void SaveCameraSet(CameraParameter parameter)
        {
            List<CameraParameter> parameters = new List<Config.CameraParameter>();
            string fp = Application.StartupPath + "\\CameraSet.json";
            if (File.Exists(fp))
            {
                parameters = JsonConvert.DeserializeObject<List<CameraParameter>>(File.ReadAllText(fp));
                File.Delete(fp);
                var info = parameters.FirstOrDefault(t => t.CameraName == parameter.CameraName);
                if (info == null)
                {
                    parameters.Add(parameter);
                }
                else
                {
                    ObjectCloneHelper.Copy(info, parameter);
                }
                File.WriteAllText(fp, JsonConvert.SerializeObject(parameters));
            }
            else
            {
                parameters.Add(parameter);
                File.WriteAllText(fp, JsonConvert.SerializeObject(parameters));
            }
        }
    }
}
